﻿using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace FlickrSL.Data
{
    public class PhotoContext : IXmlSerializable
    {
        private static readonly XmlSerializer PrevPhotoSerializer;
        private static readonly XmlSerializer NextPhotoSerializer;

        static PhotoContext()
        {
            PrevPhotoSerializer = new XmlSerializer(typeof(RelatedPhoto), CreateXmlAttributeOverrides("prevphoto"));
            NextPhotoSerializer = new XmlSerializer(typeof(RelatedPhoto), CreateXmlAttributeOverrides("nextphoto"));
        }

        private static XmlAttributeOverrides CreateXmlAttributeOverrides(String name)
        {
            XmlAttributeOverrides overrides = new XmlAttributeOverrides();
            XmlAttributes attributes = new XmlAttributes();
            attributes.XmlType = new XmlTypeAttribute(name);
            overrides.Add(typeof(RelatedPhoto), attributes);
            return overrides;
        }

        public Int32 Count { get; set; }

        public RelatedPhoto PreviousPhoto { get; set; }

        public RelatedPhoto NextPhoto { get; set; }

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            reader.MoveToContent();

            if (!reader.IsStartElement("count"))
            {
                throw new InvalidOperationException("Expected Element 'count' Not Found");
            }

            this.Count = reader.ReadElementContentAsInt();

            reader.Skip();
            reader.MoveToContent();

            while (reader.IsStartElement())
            {
                if (String.Equals(reader.Name, "prevphoto", StringComparison.OrdinalIgnoreCase))
                {
                    this.PreviousPhoto = (RelatedPhoto)PrevPhotoSerializer.Deserialize(reader);
                }
                else if (String.Equals(reader.Name, "nextphoto", StringComparison.OrdinalIgnoreCase))
                {
                    this.NextPhoto = (RelatedPhoto)NextPhotoSerializer.Deserialize(reader);
                }
                else
                {
                    throw new InvalidOperationException("Unrecognized Element '" + reader.Name + "' Found");
                }

                reader.MoveToContent();
            }
        }

        public void WriteXml(XmlWriter writer)
        {
            throw new NotImplementedException();
        }
    }

    [XmlType]
    public class RelatedPhoto
    {
        [XmlAttribute("id")]
        public Int64 Id { get; set; }

        [XmlAttribute("secret")]
        public String Secret { get; set; }

        [XmlAttribute("server")]
        public Int32 Server { get; set; }

        [XmlAttribute("farm")]
        public Int32 ServerFarm { get; set; }

        [XmlAttribute("title")]
        public String Title { get; set; }

        [XmlAttribute("url")]
        public String Url { get; set; }

        [XmlAttribute("thumb")]
        public String ThumbnailUrl { get; set; }

        [XmlAttribute("media")]
        public String MediaType { get; set; }
    }
}
